package com.txm.controller;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.model.FileInfo;
import common.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tangxinmao on 2016/11/22.
 */
@Controller
@RequestMapping("blog")
public class BlogController extends BaseController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping(value = "listimage", method = RequestMethod.POST)
    @ResponseBody
    public Map listimage() throws QiniuException {
        List<Map> imageList = new ArrayList();
        FileInfo[] items = UploadUtil.listFiles();
        for (FileInfo fileInfo : items) {
            Map image = new HashMap();
            image.put("state", "SUCCESS");
            image.put("url", redisTemplate.opsForHash().get("properties", "qn") + fileInfo.key);
            imageList.add(image);
        }
        Map listimage = new HashMap();
        listimage.put("list", imageList);
        listimage.put("total", imageList.size());
        listimage.put("state", "SUCCESS");
        listimage.put("start", 0);
        return listimage;
    }
}
